package com.example.villagemarket;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import com.example.villagemarket.MainActivity.getResults;
import com.example.villagemarket.adapter.MenuAdapter;
import com.example.villagemarket.adapter.MenuListView;
import com.example.villagemarket.adapter.ShoppingCartAdapter;
import com.example.villagemarket.adapter.ShoppingCartListView;
import com.haarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;

import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.view.Gravity;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CalendarView.OnDateChangeListener;

public class FoodScheduleFragment extends Fragment {

	public static final String DATE_FORMAT_NOW = "MM-dd-yyyy";

	List<Map<String, String>> itemHolder;

	ScaleInAnimationAdapter ScaleInAnimationAdapter;

	public HashMap<String, String> items = new HashMap<String, String>();

	private View view;

	String menu_items;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (view != null) {
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null)
				parent.removeView(view);
		}
		try {
			view = inflater.inflate(R.layout.activity_food_schedule_fragment,
					container, false);
		} catch (InflateException e) {
		}
		if (haveNetworkConnection()) {
			try {
				menu_items = new getFood().execute().get();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			JsonMenuParser jmp = new JsonMenuParser();

			try {
				items = jmp.parse(menu_items);
				
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(items.size() == 0)
			{
				TextView ney = (TextView)view.findViewById(R.id.noEntryYet);
				ney.setVisibility(1);
				ney.setText("No menu has been entered for today! Please call 920-922-2265 to hear what we have to offer.");
				ney.setGravity(Gravity.CENTER);
			}
			else
			{
				// Get listview to pass
				ListView list = (ListView) view.findViewById(R.id.menu);
				

				MenuListView mlv = new MenuListView();
				itemHolder = new ArrayList<Map<String, String>>();

				for (Map.Entry<String, String> entry : items.entrySet()) {
					String key = entry.getKey();
					itemHolder.add(addItem(key, entry.getValue()));
				}
				ArrayList<HashMap<String, String>> items = mlv.populateListView(
						itemHolder, list);

				MenuAdapter ad = new MenuAdapter(getActivity()
						.getApplicationContext(), items, getActivity());

				ScaleInAnimationAdapter = new ScaleInAnimationAdapter(ad);

				ScaleInAnimationAdapter.setAbsListView(list);
				list.setAdapter(ScaleInAnimationAdapter);
				
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
				Date date = new Date();
				TextView mt = (TextView)view.findViewById(R.id.menuTitle);
				mt.setText("Grab N Go Menu for " + dateFormat.format(date));

			}
			

			
	
		}

		return view;

	}

	// helper method used to add items in data structure that holds the items
	private HashMap<String, String> addItem(String key, String value) {
		HashMap<String, String> x = new HashMap<String, String>();
		
		x.put("item", key + ":" + value);

		return x;
	}

	// checks if user has either wifi/mobile connection
	private boolean haveNetworkConnection() {
		boolean haveConnectedWifi = false;
		boolean haveConnectedMobile = false;

		ConnectivityManager cm = (ConnectivityManager) getActivity()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] netInfo = cm.getAllNetworkInfo();
		for (NetworkInfo ni : netInfo) {
			if (ni.getTypeName().equalsIgnoreCase("WIFI"))
				if (ni.isConnected())
					haveConnectedWifi = true;
			if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
				if (ni.isConnected())
					haveConnectedMobile = true;
		}
		return haveConnectedWifi || haveConnectedMobile;
	}

	// Async task to retrieve the menu for the day
	public class getFood extends AsyncTask<String, Context, String> {

		@Override
		protected String doInBackground(String... params) {

			HTTPPost hp = new HTTPPost(null,"http://worthagoogle.brosnetwork.co.uk/android_php/VM/retrieve_menu.php");

			return hp.responseString;

		}

		@Override
		protected void onPostExecute(String values) {

		}
	}

}
